import {
	TabExpViewActionType,
	ITabExpViewAbility,
	ITabExpViewController,
	ITabExpViewStore,
	ITabExpViewControllerParams,
	IParam,
	ITabExpPanelAbility,
	TabExpPanelActionType,
	IViewActionResult,
} from '@/core/interface';
import { deepCopy } from '@/core/utils';
import { ViewController } from './view-controller';

/**
 * 分页导航视图控制器
 *
 * @export
 * @class TabExpViewController
 * @extends {ExpViewController<TabExpViewActionType, ITabExpViewStore, ITabExpViewAbility>}
 * @implements {ITabExpViewController<ITabExpViewStore, ITabExpViewAbility>}
 */
export class TabExpViewController
	extends ViewController<
		TabExpViewActionType,
		ITabExpViewStore,
		ITabExpViewAbility
	>
	implements ITabExpViewController<ITabExpViewStore, ITabExpViewAbility>
{
	/**
	 * @description 处理视图初始化
	 * @protected
	 * @param {ITabExpViewControllerParams<TabExpViewActionType, ITabExpViewAbility>} params
	 * @memberof TabExpViewController
	 */
	protected processViewInit(
		params: ITabExpViewControllerParams<
			TabExpViewActionType,
			ITabExpViewAbility
		>
	) {
		super.processViewInit(params);
		Object.assign(this.store, { dataInfo: '' });
	}

	/**
	 * @description 获取分页导航面板能力
	 * @private
	 * @return {*}  {(ITabExpPanelAbility | undefined)}
	 * @memberof TabExpViewController
	 */
	protected getMainCtrlAbility(): ITabExpPanelAbility | undefined {
		const tabExpPanel = this.model.ctrls.find(
			(ctrl: IParam) => ctrl.controlType === 'TABEXPPANEL'
		);
		if (tabExpPanel) {
			return this.getSubAbility<ITabExpPanelAbility>(tabExpPanel.name);
		}
	}

	/**
	 * @description 视图加载
	 * @param {IParam} [opts={}]
	 * @return {*}
	 * @memberof TabExpViewController
	 */
	public viewMounted(opts: IParam = {}) {
		super.viewMounted(opts);
		if (this.model.useDefaultLayout) {
			if (!this.isLoadDefault) {
				this.isLoadDefault = true;
				return;
			}
            setTimeout(() => {
                this.load();
            })
		} else {
			this.initLayout().then(() => {
                if (!this.isLoadDefault) {
                    this.isLoadDefault = true;
                    return;
                }
                setTimeout(() => {
                    this.load();
                })
			});
		}
		this.getViewData();
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [args]
	 * @return {*}  {Promise<IViewActionResult>}
	 * @memberof TabExpViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		const tabExpPanel = this.getMainCtrlAbility();
		if (tabExpPanel) {
			return tabExpPanel.load();
		} else {
			return Promise.reject({
				ok: false,
				data: null,
				rowData: { status: 500 },
			});
		}
	}

	/**
	 * @description 获取视图数据
	 * @memberof TabExpViewController
	 */
	public async getViewData() {
		const entityCodeName = this.model.entityCodeName;
		if (entityCodeName) {
			if (
				this.store.context &&
				this.store.context[entityCodeName.toLowerCase()]
			) {
				const service = await App.getDataService(
					entityCodeName,
					this.store.context
				);
				if (service && service.Get) {
					const response = await service.Get(this.store.context, {});
					if (!response.success) {
						App.getNotificationHelper().error(App.ts("app.notificationtitle.error", "错误"), App.ts("app.error.unknownerror", "发生位置错误"));
						return;
					}
					const data = response.data;
					if (data) {
						this.store.dataInfo = data[entityCodeName.toLowerCase()];
					}
				}
			}
		}
	}

	/**
	 * 处理部件行为
	 *
	 * @param {string} name
	 * @param {(TabExpPanelActionType)} action
	 * @param {IParam[]} data
	 * @memberof TabExpViewController
	 */
	public handleCtrlAction(
		name: string,
		action: TabExpPanelActionType,
		data: IParam[]
	): void {
		const tabExpPanel = this.getMainCtrlAbility();
		if (tabExpPanel && name === tabExpPanel.name) {
			this.handleTabExpPanelAction(action as TabExpPanelActionType, data);
			return;
		}
	}

	/**
	 * 处理分页导航面板行为
	 *
	 * @param {string} name
	 * @param {TabExpPanelActionType} action
	 * @param {IParam[]} data
	 * @memberof TabExpViewController
	 */
	public handleTabExpPanelAction(
		action: TabExpPanelActionType,
		data: IParam[]
	) {
		if (action === 'tabClick') {
			this.loadTabExpPanelData();
		}
	}

	/**
	 * 加载TabExpPanel数据
	 *
	 * @memberof TabExpViewController
	 */
	public loadTabExpPanelData() {
		const tabExpPanel = this.getMainCtrlAbility();
		const tempViewParams = deepCopy(this.store.viewParams);
		if (tabExpPanel) {
			tabExpPanel.load(tempViewParams);
		}
	}
}
